<?php

class BaseJob {

	protected $job;
	protected $taskService;
	
	public function __construct($job) {			
		$this->job = $job;
		$this->taskService = TaskService::getInstance();
	}
	
	public function canTrigger($taskDate) {
		if($this->job['job_frequency'] == 0 && isset($this->job['trigger_param']) && !empty($this->job['trigger_param'])) {
			$this->job['trigger_param'] = CJSON::decode($this->job['trigger_param']);
			return $this->job['trigger_param']['auto'] == true;
		}
        return true;
	}
	
	public function getFreqency() {
		return $this->job['job_frequency'];
	}

	public function createTask($taskDate) {
		$task = array(
			'job_id'	=>	$this->job['job_id'],
			'date'		=>	$taskDate,
			'state'		=>	TaskState::Waiting,
			'task_type'	=>	$this->job['job_type'],
			'workflow_name'=>	$this->job['workflow_name'],
			'frequency'	=>	$this->job['job_frequency'],
			'priority'	=>	$this->job['job_priority'],
		);
		return $this->taskService->addTask($task);
	}
}
